package com.tian.hello.ui.topline;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

import com.tian.hello.R;
import com.tian.hello.ui.topline.base.IViewModel;
import com.tian.hello.ui.topline.base.ItemInfo;
import com.tian.hello.ui.topline.base.MDecoration;
import com.tian.hello.ui.topline.model.FriendViewModel;
import com.tian.hello.ui.topline.model.ImageModel;
import com.tian.hello.ui.topline.model.TxtModel;
import com.tian.hello.ui.topline.model.VedioModel;

/**
 * 复杂列表布局页， 通过添加接口IViewModel的实现类如TxtModel,ImageModel)来达到对多种不同itemview布局的实现。
 * <p>@author：skyTian
 * <br>@time:2018/2/1
 * <br>@copyright:com.sky
 */
public class ComplexListActivity extends AppCompatActivity {
    private RecyclerView message_rv;
    private ComplexListAdapter adapter;
    private TextView btn_one;

    private Button btn_two;

    private ArrayList<ItemInfo> itemInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complx_rc);
        initRc();
    }

    private void initRc() {
        btn_one = findViewById(R.id.btn_one);
        btn_two = findViewById(R.id.btn_two);
        btn_one.setText("对多种item类型布局的recyleView的展示,实现思路：\n1.通过添加接口IViewModel的实现类如(TxtModel,ImageModel)来达到对多种不同itemview布局的实现。" +
                "\n2.这里是典型的开闭原则，添加新的布局，是以添加新的类去实现的，避免在activity或者adapter中各种过多的改动.类似这种功能的实现一定要多考虑程序拓展,不能只图实现功能的没任何意义；");

        message_rv = findViewById(R.id.message_rv);
        itemInfos = new ArrayList<>();

        itemInfos.add(new ItemInfo(IViewModel.MODEL_IMG));
        itemInfos.add(new ItemInfo(IViewModel.MODEL_VEDIO));
        itemInfos.add(new ItemInfo(IViewModel.MODEL_FRIEND));
        itemInfos.add(new ItemInfo(IViewModel.MODEL_TXT));
        itemInfos.add(new ItemInfo(IViewModel.MODEL_IMG));
        itemInfos.add(new ItemInfo(IViewModel.MODEL_VEDIO));
        itemInfos.add(new ItemInfo(IViewModel.MODEL_TXT));
        itemInfos.add(new ItemInfo(IViewModel.MODEL_IMG));
        //简便模式
        adapter = new ComplexListAdapter(this, itemInfos);
        message_rv.addItemDecoration(new MDecoration(this, MDecoration.VERTICAL_LIST));
        message_rv.setLayoutManager(new LinearLayoutManager(this));
        message_rv.setAdapter(adapter);

        btn_two.setOnClickListener(v -> showItemView());
    }

    /**
     * 演示动态添加
     */
    private void showItemView() {

        itemInfos.clear();
        adapter.notifyDataSetChanged();
        adapter.clearModels();

        //演示动态添加
        itemInfos.add(new ItemInfo(IViewModel.MODEL_IMG));
        adapter.addModel(new ImageModel());

        itemInfos.add(new ItemInfo(IViewModel.MODEL_VEDIO));
        adapter.addModel(new VedioModel());

        itemInfos.add(new ItemInfo(IViewModel.MODEL_TXT));
        adapter.addModel(new TxtModel());

        itemInfos.add(new ItemInfo(IViewModel.MODEL_FRIEND));
        adapter.addModel(new FriendViewModel());

        /**
         * adapter已有数据类型ViewModel就在需要添加了
         */
        itemInfos.add(new ItemInfo(IViewModel.MODEL_IMG));
        itemInfos.add(new ItemInfo(IViewModel.MODEL_FRIEND));

        adapter.notifyDataSetChanged();

        btn_two.setText("页面结果显示，是动态改变数据和adapter的结果");

    }


}
